package com.jdy.haoduoai.view.calendar.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.jdy.haoduoai.util.Tools;
import com.jdy.haoduoai.view.calendar.abs.RefreshListener;
import com.jdy.haoduoai.view.calendar.adpter.MonthCalendarAdpter;
import com.jdy.haoduoai.view.calendar.adpter.WeekCalendarAdpter;
import com.jdy.haoduoai.view.calendar.adpter.WeekCalendarAdpter.OnDateSelectListener;
import com.jdy.haoduoai.view.calendar.fragment.widget.HandMoveLayout;
import com.jdy.haoduoai.view.calendar.fragment.widget.HasTwoAdapterViewpager;
import com.jdy.haoduoai.view.calendar.utils.DateUtils;
import com.ycsj.goldmedalnewconcept.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

//import com.jdy.haoduoai.R;

/**
 * 我的日程
 */
public class MyCalendarFragment extends Fragment implements RefreshListener,
    OnDateSelectListener {
  public static final int back_code = 121;

  public MyCalendarFragment() {
    super();
  }

  public OnDateSelectListener mOnDateSelectListener;

  public void setOnDateSelectListener(OnDateSelectListener mOnDateSelectListener) {
    this.mOnDateSelectListener = mOnDateSelectListener;
  }

  private HandMoveLayout handMoveLayout;
  public static boolean showMonthView;

  @SuppressLint("ValidFragment")
  public MyCalendarFragment(Handler os, boolean showMonthView) {
    super();
    this.showMonthView = showMonthView;
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_my_calender, container, false);
  }

  /**
   * 调整到下个月
   * */
  public void pagerNext() {
    if (viewPager != null) {
      viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
    }
  }

  public void requestFocus() {
    if (viewPager != null) {
      viewPager.setFocusable(true);
      viewPager.setFocusableInTouchMode(true);
      viewPager.requestFocus();
    }
  }

  /**
   * 调整到上个月
   * */
  public void pagerLast() {
    if (viewPager != null) {
      viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
    }
  }

  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    handMoveLayout = (HandMoveLayout) getView().findViewById(
        R.id.handmovelayout);

    initCalendar();

  }

  public void switch2MonthView() {
    if (handMoveLayout != null) {
      handMoveLayout.switch2MonthView();
    }
  }

  public void switch2WeekView() {
    if (handMoveLayout != null) {
      handMoveLayout.switch2WeekView();
    }
  }

  public String getSelectTime() {
    if (weekCalendarAdpter != null
        && Tools.isNotEmpty(weekCalendarAdpter.getSelectTime())) {
      return weekCalendarAdpter.getSelectTime();
    } else if (adpter != null && Tools.isNotEmpty(adpter.getSelectTime())) {
      return adpter.getSelectTime();
    } else {
      return null;
    }
  }

  private HasTwoAdapterViewpager viewPager;
  private HasTwoAdapterViewpager viewpagerWeek;
  private List<View> views;
  private WeekCalendarAdpter weekCalendarAdpter;
  private ArrayList<String> timeList = new ArrayList<>();

  public void setMonthViewScrollble(boolean bCanScroll) {
    if (viewPager != null)
      viewPager.setScrollble(bCanScroll);
  }

  public String getMonthViewDateStr() {
    Calendar today = new GregorianCalendar();
    today.setTimeInMillis(System.currentTimeMillis());
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(today.getTime());
  }

  public void initCalendar() {
    viewPager = (HasTwoAdapterViewpager) getView().findViewById(
        R.id.calendar_viewpager);
    viewpagerWeek = (HasTwoAdapterViewpager) getView().findViewById(
        R.id.calendar_viewpager_week);

    if (showMonthView) {
      viewPager.setVisibility(View.VISIBLE);
    }

    viewPager.setListener(this);
    viewpagerWeek.setListener(this);

    // 制造月视图所需view
    views = new ArrayList<>();
    LinearLayout layout = (LinearLayout) View.inflate(getActivity(),
        R.layout.mouth, null);
    LinearLayout layout1 = (LinearLayout) View.inflate(getActivity(),
        R.layout.mouth, null);
    LinearLayout layout2 = (LinearLayout) View.inflate(getActivity(),
        R.layout.mouth, null);
    LinearLayout layout3 = (LinearLayout) View.inflate(getActivity(),
        R.layout.mouth, null);
    views.add(layout);
    views.add(layout1);
    views.add(layout2);
    views.add(layout3);

    adpter = new MonthCalendarAdpter(views, getActivity(), timeList);
    adpter.setHandler(os);
    adpter.setOnDateSelectListener(this);

    // 制造日试图所需view
    List viewss = new ArrayList();
    LinearLayout layoutri = (LinearLayout) View.inflate(getActivity(),
        R.layout.week, null);
    LinearLayout layout1ri = (LinearLayout) View.inflate(getActivity(),
        R.layout.week, null);
    LinearLayout layout2ri = (LinearLayout) View.inflate(getActivity(),
        R.layout.week, null);
    LinearLayout layout3ri = (LinearLayout) View.inflate(getActivity(),
        R.layout.week, null);
    viewss.add(layoutri);
    viewss.add(layout1ri);
    viewss.add(layout2ri);
    viewss.add(layout3ri);
    weekCalendarAdpter = new WeekCalendarAdpter(viewss, getActivity(), timeList);
    weekCalendarAdpter.setHandler(os);
    weekCalendarAdpter.setOnDateSelectListener(this);
    viewPager.setAdapter(adpter);
    viewPager.setCurrentItem(1200, true);
    viewpagerWeek.setAdapter(weekCalendarAdpter);
    viewpagerWeek.setCurrentItem(weekCalendarAdpter.getCount() / 2);

    // 如果是周日，就翻到下一页
    Calendar today = new GregorianCalendar();
    today.setTimeInMillis(System.currentTimeMillis());

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset,
          int positionOffsetPixels) {
        Log.d("position", Integer.toString(position));

      }

      @Override
      public void onPageSelected(int position) {
        Calendar today = new GregorianCalendar();
        today.setTimeInMillis(System.currentTimeMillis());
        // 距离当前时间的月数(按月算)
        int month = adpter.getCount() / 2 - position;
        today.add(Calendar.MONTH, -month);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String strToday = sdf.format(today.getTime());

        if (mOnDateSelectListener != null) {
          mOnDateSelectListener.changeDate(strToday, "month");
        }

        // setBarTitle(getTopTitleTime(today));
        // 更新currentItem
        // viewPager.setTag(R.id.month_current,position);

        adpter.instantiateItem(viewPager, viewPager.getCurrentItem());

      }

      @Override
      public void onPageScrollStateChanged(int state) {

      }
    });
    viewpagerWeek.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset,
          int positionOffsetPixels) {

      }

      @SuppressLint("SimpleDateFormat")
      @Override
      public void onPageSelected(int position) {
        Calendar today = new GregorianCalendar();
        today.setTimeInMillis(System.currentTimeMillis());

        int day_of_week = today.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
          day_of_week = 7;
        }
        today.add(Calendar.DAY_OF_WEEK, -day_of_week);
        // 距离当前时间的周数(按周算)
        int week = weekCalendarAdpter.getCount() / 2 - position;
        today.add(Calendar.DAY_OF_WEEK, -week * 7);
        // setBarTitle(getTopTitleTime(today));
        // 刷新本页

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String strToday = sdf.format(today.getTime());

        int currentItem = getWeekCurrentItem();

        if (mOnDateSelectListener != null) {
          mOnDateSelectListener.changeDate(strToday, "week");
        }

        weekCalendarAdpter.instantiateItem(viewpagerWeek,
            viewpagerWeek.getCurrentItem());
      }

      @Override
      public void onPageScrollStateChanged(int state) {

      }
    });
  }

  private MonthCalendarAdpter adpter;
  /**
   * 用于接收上面日期改变的消息
   */
  public static final int change = 90;
  public static final int change2 = 91;

  public static final int pagerNext = 101;
  public static final int pagerLast = 102;
  Handler os = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      if (msg.what == 90) {
        // do same thing
      } else if (msg.what == change2) {
        handMoveLayout.setRowNum((Integer) msg.obj);
      } else if (msg.what == pagerNext) {
        pagerNext();
      } else if (msg.what == pagerLast) {
        pagerLast();
      }
    }
  };

  int currentItem = 0;

  @Override
  public void refreshListener(final ViewPager viewPager) {
    // ToastUtils.shortMsg("刷新");
    // 得到这个selecttime对应的currentItem
    currentItem = 0;
    if (viewPager.getAdapter() instanceof MonthCalendarAdpter) {
      adpter.getTimeList(timeList);
      // 月视图
      currentItem = getMonthCurrentItem();
      int odl = viewPager.getCurrentItem();
      viewPager.setCurrentItem(currentItem, false);
      // 刷新已经存在的3个视图view
      if (Math.abs(odl - currentItem) <= 1) {
        adpter.instantiateItem(viewPager, viewPager.getCurrentItem() - 1);

        adpter.instantiateItem(viewPager, viewPager.getCurrentItem());

        adpter.instantiateItem(viewPager, viewPager.getCurrentItem() + 1);
      }
      adpter.notifyDataSetChanged();
    } else {
      // 周视图
      currentItem = getWeekCurrentItem();
      // 如果是周日，就是下一周，+1
      if (DateUtils.getWeekStr(DateUtils.stringToDate(adpter.getSelectTime()))
          .equals("星期日")) {
        currentItem++;
      }
      weekCalendarAdpter.getTimeList(timeList);
      int odl = viewPager.getCurrentItem();
      viewPager.setCurrentItem(currentItem, false);
      // 刷新已经存在的3个视图view
      if (Math.abs(odl - currentItem) <= 1) {
        weekCalendarAdpter.instantiateItem(viewPager,
            viewPager.getCurrentItem() - 1);

        weekCalendarAdpter.instantiateItem(viewPager,
            viewPager.getCurrentItem());

        weekCalendarAdpter.instantiateItem(viewPager,
            viewPager.getCurrentItem() + 1);
      }
      weekCalendarAdpter.notifyDataSetChanged();
    }

  }

  // 得到月视图选中日期后的CurrentItem
  private int getMonthCurrentItem() {
    // 此刻
    Calendar today = new GregorianCalendar();
    today.setTimeInMillis(System.currentTimeMillis());
    // 选中时间
    String time = adpter.getSelectTime();
    Date date = DateUtils.stringToDate(time);
    Calendar sele = new GregorianCalendar();
    sele.setTimeInMillis(date.getTime());

    // 选中时间的(MONTH)-此刻(MONTH)=月数
    int aa = sele.get(Calendar.MONTH) - today.get(Calendar.MONTH);

    int result = sele.get(Calendar.MONTH) - today.get(Calendar.MONTH);
    int month = (sele.get(Calendar.YEAR) - today.get(Calendar.YEAR)) * 12;
    aa = month + result;
    return adpter.getCount() / 2 + aa;
  }

  // 得到周视图选中日期后的CurrentItem
  public int getWeekCurrentItem() {
    // 此刻
    Calendar today = new GregorianCalendar();
    today.setTimeInMillis(System.currentTimeMillis());
    // 转为本周一
    int day_of_week = today.get(Calendar.DAY_OF_WEEK) - 1;
    if (day_of_week == 0) {
      day_of_week = 7;
    }
    today.add(Calendar.DATE, -day_of_week);
    // 选中时间
    String time = weekCalendarAdpter.getSelectTime();
    Date date = DateUtils.stringToDate(time);
    Calendar sele = new GregorianCalendar();
    sele.setTimeInMillis(date.getTime());

    // 选中时间的(day of yeay)-此刻(day of yeay)=天数
    int aa = ((int) (sele.getTime().getTime() / 1000) - (int) (today.getTime()
        .getTime() / 1000)) / 3600 / 24;
    int aa2 = 0;
    if (Math.abs(aa) % 7 == 0) {
      aa2 = Math.abs(aa) / 7;
    } else {
      aa2 = Math.abs(aa) / 7;
    }
    if (aa >= 0) {
      return weekCalendarAdpter.getCount() / 2 + aa2;
    } else {
      return weekCalendarAdpter.getCount() / 2 - aa2 - 1;
    }
  }

  public String mSelectDateStr;

  @Override
  public void onDateSelected(String date) {
    mSelectDateStr = date;
    if (mOnDateSelectListener != null) {
      mOnDateSelectListener.onDateSelected(date);
    }
  }

  // @Override
  // public void changeDate(String date) {
  // // TODO Auto-generated method stub
  //
  // }

  @Override
  public void changeDate(String date, String viewType) {
    // TODO Auto-generated method stub

  }
}
